
import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Controls 2.14
import QtCharts 2.14
import Mydata 1.0

Window {
    id: winroot
    visible: true
    width: 800
    height: 480
    title: qsTr("智能家居室内环境监控系统")

    property bool switchbool: true
    property var boolshebei: [false,false,false,false,false]

    property int myindex: 1

    property int wdmax
    property int sdmax
    property int gzmax
    property int krqtmax
    property int wdmin
    property int sdmin
    property int gzmin
    property int krqtmin

    property int bzwd: 20
    property int bzsd: 50
    property int bzgz: 50
    property int bzqt: 0


    property var ans : [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]

    property int wd: rootdata.wd
    property int sd: rootdata.sd
    property int gz: rootdata.gz
    property int krqt: rootdata.qt

    //主界面显示代理
    Loader {
        id: rootLoader
        anchors.fill: parent
        onSourceChanged: {
            wl.visible = false
            zn.visible = false
        }
        MouseArea{
            anchors.fill: parent
            z:-1
            onClicked: {
                wl.visible = false
                zn.visible = false
            }
        }
    }

    //网络连接与智能调解小窗口
    Rectangle{
        id: zn
        width: 200
        height: 150
        radius: 10
        color: "#ffffff"
        anchors.top: wlloader.bottom
        anchors.topMargin:  10
        anchors.right: parent.right
        visible: false

        Switch {
            id: swi
            anchors.right: parent.right
            anchors.rightMargin: 10
            checked: winroot.switchbool
            onCheckedChanged: {
                if (swi.checked) {
                    rootdata.znkz = 0;
                    winroot.switchbool = true;
                    rootdata.my_send(1, winroot.bzwd, winroot.bzsd, winroot.bzgz, winroot.bzqt)
                } else {
                    rootdata.znkz = -1;
                    winroot.switchbool = false
                    rootdata.my_send(1,-1,-1,-1,-1)
                }
            }
        }
        MouseArea{
            anchors.fill: parent
            z:-1
        }

        Text {
            id: xx
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.verticalCenter: swi.verticalCenter
            text: "智能调节"
        }
        Text {
            id: name
            anchors.top: swi.bottom
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.verticalCenter: parent.verticalCenter
            text: (rootdata.znkz != -1 ? ("标准温度："+bzwd+"℃"+"\n"+"标准湿度："+bzsd+"%"+"\n"+"标准光照强度："+bzgz+"%"+"\n"+"标准可燃气体浓度："+bzqt+"%"+"\n") : ("智能调节未打开"))
        }
    }
    Rectangle{
        id: wl
        width: 200
        height: 50
        radius: 10
        color: "#ffffff"
        anchors.top: wlloader.bottom
        anchors.topMargin:  10
        anchors.right: parent.right
        visible: false

        MouseArea{
            anchors.fill: parent
            z:-1
        }
        Text {
            id: tex
            anchors.verticalCenter: parent.verticalCenter
            anchors.left: parent.left
            anchors.leftMargin: 10
            text: "网络连接权限"
            color: "#000000"
        }
        Switch {
            id: swit
            anchors.right: parent.right
            anchors.rightMargin: 10
            anchors.verticalCenter: parent.verticalCenter
            checked: true
            onCheckedChanged: {
                if (swit.checked) {
                    rootdata.myaccept()
                } else {
                    rootdata.myclose()
                }
            }
        }
    }
    Image {
        id: wlloader
        anchors.top: parent.top
        anchors.right: parent.right
        anchors.rightMargin: 20
        Component.onCompleted: {
            source = (rootdata._client == 0 ? "6.bmp" : "7.bmp")
        }
        MouseArea {
            anchors.fill: parent
            onClicked: {
                zn.visible = false
                wl.visible = (wl.visible == false ? true : false)
            }
        }
    }
    Image {
        id: znloader
        anchors.top: parent.top
        anchors.right: wlloader.left
        anchors.rightMargin: 20
        Component.onCompleted: {
            source = (rootdata.znkz == -1 ? "9.bmp" : "8.bmp")
        }
        MouseArea {
            anchors.fill: parent
            onClicked: {
                wl.visible = false
                zn.visible = (zn.visible == false ? true : false)
            }
        }
    }

    //数据采集模块
    MyData{
        id: rootdata
        onMyinitChanged: {
            my_send(rootdata.znkz, winroot.bzwd, winroot.bzsd, winroot.bzgz, winroot.bzqt, winroot.boolshebei[0], winroot.boolshebei[1], winroot.boolshebei[2], winroot.boolshebei[3])
        }
        //网络和智能控制改变
        on_ClientChanged: {
            if (_client != 0) {
                wlloader.source = "7.bmp"
            } else {
                wlloader.source = "6.bmp"
            }
        }
        onZnkzChanged: {
            if (znkz != -1) {
                znloader.source = "8.bmp"
                switchbool = true
            } else {
                znloader.source = "9.bmp"
                switchbool = false
            }
        }

        //开关改变
        property int index1: 0
        property int index2: 0
        property int index3: 0
        property int index4: 0
        onGz_on_offChanged: {
            if (gz_on_off == 1) {
                winroot.boolshebei[0] = true
            } else {
                winroot.boolshebei[0] = false
            }
            if (winroot.myindex == 0){
                rootLoader.source = "Huanjingjkong.qml"
                rootLoader.source = "Shebeikongzhi.qml"
            }
        }
        onSd_on_offChanged: {
            if (sd_on_off == 1) {
                winroot.boolshebei[1] = true
            } else {
                winroot.boolshebei[1] = false
            }
            if (winroot.myindex == 0){
                rootLoader.source = "Huanjingjkong.qml"
                rootLoader.source = "Shebeikongzhi.qml"
            }
        }
        onWd_on_offChanged: {
            if (wd_on_off == 1) {
                winroot.boolshebei[2] = true
            } else {
                winroot.boolshebei[2] = false
            }
            if (winroot.myindex == 0){
                rootLoader.source = "Huanjingjkong.qml"
                rootLoader.source = "Shebeikongzhi.qml"
            }
        }
        onQt_on_offChanged: {
            if (qt_on_off == 1) {
                winroot.boolshebei[3] = true
            } else {
                winroot.boolshebei[3] = false
            }
            if (winroot.myindex == 0){
                rootLoader.source = "Huanjingjkong.qml"
                rootLoader.source = "Shebeikongzhi.qml"
            }
        }

        //数据改变
        onWdChanged: {
            winroot.wdmin = (winroot.wdmin < wd ? winroot.wdmin : wd)
            winroot.wdmax = (winroot.wdmax > wd ? winroot.wdmax : wd)
            if (index1 == 30) {
                index1 = 29;
                for (var i = 0; i < 29; i++) {
                    ans[0][i] = ans[0][i+1];
                }
            }
            ans[0][index1++] = wd;
        }
        onSdChanged: {
            winroot.sdmin = (winroot.sdmin < sd ? winroot.sdmin : sd)
            winroot.sdmax = (winroot.sdmax > sd ? winroot.sdmax : sd)
            if (index2 == 30) {
                index2 = 29;
                for (var i = 0; i < 29; i++) {
                    ans[1][i] = ans[1][i+1];
                }
            }
            ans[1][index2++] = sd;
        }
        onGzChanged: {
            winroot.gzmin = (winroot.gzmin < gz ? winroot.gzmin : gz)
            winroot.gzmax = (winroot.gzmax > gz ? winroot.gzmax : gz)
            if (index3 == 30) {
                index3 = 29;
                for (var i = 0; i < 29; i++) {
                    ans[2][i] = ans[2][i+1];
                }
            }
            ans[2][index3++] = gz;
        }
        onQtChanged: {
            winroot.krqtmin = (winroot.krqtmin < qt ? winroot.krqtmin : qt)
            winroot.krqtmax = (winroot.krqtmax > qt ? winroot.krqtmax : qt)
            if (index4 == 30) {
                index4 = 29;
                for (var i = 0; i < 29; i++) {
                    ans[3][i] = ans[3][i+1];
                }
            }
            ans[3][index4++] = qt;
        }
    }
    function myqmlexit() {
        rootdata.myexit();
    }

    //初始化
    Component.onCompleted: {
        rootLoader.source = "Zhujiemian.qml"
        winroot.wdmax = -1
        winroot.sdmax = -1
        winroot.gzmax = -1
        winroot.krqtmax = -1
        winroot.wdmin = 101
        winroot.sdmin = 101
        winroot.gzmin = 101
        winroot.krqtmin = 101
    }


}
